package com.wuliqing.android.zte.view;

import android.content.Context;
import android.graphics.AvoidXfermode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.wuliqing.android.zte.myapplication.R;

/**
 * Created by 10172915 on 2015/7/9.
 */
public class CustomView extends View {
    private int mWidth;
    private int mHeight;
    private Paint mPaint;
    private Context mContext;
    private Bitmap mBitmap;
    private PorterDuffXfermode porterDuffXfermode;// 图形混合模式
    private int x, y, w, h;
    ;
    //    ColorMatrix colorMatrix = new ColorMatrix(new float[]{
//            0.33F, 0.59F, 0.11F, 0, 0,
//            0.33F, 0.59F, 0.11F, 0, 0,
//            0.33F, 0.59F, 0.11F, 0, 0,
//            0, 0, 0, 1, 0,
//    });
    ColorMatrix colorMatrix = new ColorMatrix(new float[]{
            -1, 0, 0, 1, 1,
            0, -1, 0, 1, 1,
            0, 0, -1, 1, 1,
            0, 0, 0, 1, 0,
    });


    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mWidth = dm.widthPixels;
        mHeight = dm.heightPixels;
        porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SCREEN);
        initPaint();
        initRes();
    }

    private void initRes() {
        mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.cheese_3);
        x = mWidth / 2 - mBitmap.getWidth() / 2;
        y = mHeight / 2 - mBitmap.getHeight() / 2;
        w = mWidth / 2 + mBitmap.getWidth() / 2;
        h = mHeight / 2 + mBitmap.getHeight() / 2;
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mPaint.setStyle(Paint.Style.FILL);
//        mPaint.setColor(Color.argb(255, 255, 128, 103));
//        mPaint.setStrokeWidth(10);
//        mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
//        mPaint.setColorFilter(new LightingColorFilter(0xFFFF00FF, 0x00000000));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //                    mPaint.setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0X0000FF00));
//                    mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
                mPaint.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.DARKEN));
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                mPaint.setColorFilter(null);
                invalidate();
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        int sc = canvas.saveLayer(0, 0, mWidth, mHeight, null, Canvas.ALL_SAVE_FLAG);
        canvas.drawColor(0xcc1c093e);
        mPaint.setXfermode(porterDuffXfermode);
        canvas.drawBitmap(mBitmap, x, y, mPaint);
        mPaint.setXfermode(null);
        canvas.restoreToCount(sc);
    }
}
